package com.zb.sannuo.account.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zb.sannuo.account.service.AccountAddService;
import com.zb.sannuo.account.service.AccountPageService;
import com.zb.sannuo.account.service.AccountService;
import com.zb.sannuo.account.service.AccountUpdateService;
import com.zb.sannuo.common.domain.SccUserLogin;
import com.zb.sannuo.common.domain.search.LoginRoleDrugstoreSearch;
import com.zb.sannuo.common.domain.vo.AccountVo;
import com.zb.sannuo.common.result.Result;
import com.zb.sannuo.common.result.ResultCode;
import com.zb.sannuo.common.util.PasswordUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/login")
@Api(tags = "系统账号管理模块",description = "对系统账号管理进行CRUD操作" )
@CrossOrigin
public class AccountController {

    @Resource
    private AccountPageService accountPageServiceImpl;

    @Resource
    private AccountAddService accountAddServiceImpl;

    @Resource
    private AccountUpdateService accountUpdateServiceImpl;

    @Resource
    private AccountService accountServiceImpl;

    //分页
    @RequestMapping(value = "list",method = RequestMethod.GET)
    @ApiOperation(value = "系统账号列表",notes = "查询系统账号信息")
    public Result find(Integer page, Integer limit, String searchParams) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        LoginRoleDrugstoreSearch search = null;
        if (searchParams != null && !"".equals(searchParams)) {
            search = objectMapper.readValue(searchParams, LoginRoleDrugstoreSearch.class);
        }
        Page page1 = accountPageServiceImpl.selectPage(page, limit, search);
        System.out.println(page1.getTotal());
        return Result.ok(ResultCode.SUCCESS, page1.getTotal(), page1.getRecords());
    }


    //添加
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ApiOperation(value = "添加账号信息",notes = "添加账号信息",httpMethod = "POST")
    public Result accountAdd(AccountVo vo) throws Exception{
        boolean b = accountAddServiceImpl.addAccount(vo);
        if(b){
            return Result.ok(ResultCode.SUCCESS,b);
        }else {
            return Result.fail(ResultCode.FAIL,null);
        }
    }


    //修改
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ApiOperation(value = "修改账号信息",notes = "修改账号信息",httpMethod = "POST")
    public Result accountUpdate(AccountVo vo) throws Exception{
        int i = accountUpdateServiceImpl.updateAccount(vo);
        if (i>0){
            return Result.ok(ResultCode.SUCCESS,i);
        }else{
            return Result.fail(ResultCode.FAIL,null);
        }
    }


    //停用状态
    @RequestMapping(value = "{phone}",method = RequestMethod.GET)
    @ApiOperation(value = "状态的更改",notes = "状态的更改",httpMethod = "GET")
    public Result status(@PathVariable("phone") String phone) throws Exception{
        int i = accountServiceImpl.updateStatus(phone);
        if (i>0){
            return Result.ok(ResultCode.SUCCESS,i);
        }else{
            return Result.fail(ResultCode.FAIL,null);
        }
    }


}
